package com.tianqu.tms.core.taglib;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.commons.digester.Digester;
import org.xml.sax.SAXException;

import com.tianqu.tms.core.base.Logger;


/**
 * 
 * @author liugang
 *
 */
public class MenuDigester {

	private Logger logger = Logger.getLogger(MenuDigester.class);
	
/*-------------------- Constructor --------------------*/

	public MenuDigester() {
		;
	}


/*-------------------- Public methods --------------------*/

	/**
	 * 
	 */
	public MenuBean digest( String fileName )
									throws ParserConfigurationException,
											SAXException,
											IOException {

		MenuBean navigationVO     = null; 
		InputStream  in           = null; 

		try {

			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser parser = factory.newSAXParser();
			Digester digester = new Digester( parser );

			digester.addObjectCreate( "menus", MenuBean.class );
			digester.addSetProperties( "menus" );	

			digester.addObjectCreate(
				"menus/menu"
				,MenuBean.class
				);
			digester.addSetNext(
				"menus/menu"
				,"add"
				);
			digester.addSetProperties("menus/menu" );

			digester.addObjectCreate(
				"menus/menu/menu"
				, MenuBean.class
				);
			digester.addSetNext( 
				"menus/menu/menu"
				,"add"
				);
			digester.addSetProperties( "menus/menu/menu" );
			digester.addObjectCreate(
				"menus/menu/menu/menu"
				,MenuBean.class
				);
			digester.addSetNext(
				"menus/menu/menu/menu"
				,"add" );		  
			digester.addSetProperties( "menus/menu/menu/menu" );
		
			digester.addObjectCreate(
				"menus/menu/menu/menu/menu"
				, MenuBean.class );
			digester.addSetNext(
				"menus/menu/menu/menu/menu"
				,"add" );
			digester.addSetProperties( "menus/menu/menu/menu/menu" );

			in = new FileInputStream( fileName );

			navigationVO = (MenuBean)digester.parse( in );

		} catch ( Exception e ) {
			logger.error(e.toString());

		} finally {

			if ( in != null ) {
				try {
					in.close();
				} catch ( IOException ioe ) {
					;
				}
			}
		}
		return navigationVO;
	}
}